package com.sl.au.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.repository.Query;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.EventRegister;
import com.sl.au.entity.EventRegisterAndTaskView;
import com.sl.au.entity.EventRegisterTask;
import com.sl.au.entity.ViewForEventRegisterTask;
import com.sl.au.entity.ViewForTimeOutTask;
import com.sl.au.entity.EventTask;
import com.sl.au.entity.EventReport;
import com.sl.au.entity.EventToEvaluateUser;
import com.sl.au.entity.Grading;
import com.sl.au.entity.EventType;
import com.sl.au.entity.GridAreaInfo;
import com.sl.au.entity.Option;
import com.sl.au.entity.User;
import com.sl.au.entity.UserSingle;
import com.sl.au.repository.EventRegisterRepository;
import com.sl.au.repository.GradingRepository;
import com.sl.au.repository.EventTypeRepository;
import com.sl.au.repository.GridAreaInfoRepository;
import com.sl.au.repository.EventTaskRepository;
import com.sl.au.repository.EventReportRepository;
import com.sl.au.repository.OptionRepository;
import com.sl.au.repository.ViewForEventRegisterTaskRepository;
import com.sl.au.repository.EventToEvaluateUserRepository;
import com.sl.au.service.EventRegisterService;

@Service("EventRegisterService")
@Transactional
public class EventRegisterServiceImpl extends BaseServiceImpl implements EventRegisterService {

	@Autowired
	EventRegisterRepository eventRegisterRepository;
	@Autowired
	EventToEvaluateUserRepository eventToEvaluateUserRepository;
	@Resource
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private GradingRepository gradingRepository;
	@Autowired
	private EventTypeRepository eventTypeRepository;
	@Autowired
	private GridAreaInfoRepository gridAreaInfoRepository;

	@Autowired
	private EventReportRepository eventReportRepository;

	@Autowired
	private EventTaskRepository eventTaskRepository;
	@Autowired
	private OptionRepository optionRepository;

	@Autowired
	private ViewForEventRegisterTaskRepository viewForEventRegisterTaskRepository;
	
	public EventRegisterRepository getRepository() {
		return this.eventRegisterRepository;
	}

	public EventTaskRepository getTaskRepository() {
		return this.eventTaskRepository;
	}

	public EventToEvaluateUserRepository getEventToEvaluateUserRepository() {
		return this.eventToEvaluateUserRepository;
	}

	@Override
	public List<EventRegister> getPage(int start, int length, String key, String orderName, Direction dir) {
		Page<EventRegister> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.eventRegisterRepository.getByKey(key, pageable);
		} else {
			pages = this.eventRegisterRepository.findAll(pageable);
		}

		return pages.getContent();
	}

	@Override
	public List<EventRegister> getPageByEventReport(int start, int length, String eventReport, String key,
			String orderName, Direction dir) {
		Page<EventRegister> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.eventRegisterRepository.findByEventReportIdWithKey(eventReport, key, pageable);
		} else {
			pages = this.eventRegisterRepository.findByEventReportId(eventReport, pageable);
		}

		return pages.getContent();
	}

	@Override
	public List<EventRegister> getPageByStateAndUser(int start, int length, String state, String user, String key,
			String orderName, Direction dir) {
		Page<EventRegister> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			if (StringUtils.hasText(state) && StringUtils.hasText(user)) {
				pages = this.eventRegisterRepository.findByStateAndUserWithKey(state, user, key, pageable);
			} else if (StringUtils.hasText(state) && !StringUtils.hasText(user)) {
				pages = this.eventRegisterRepository.findByStateWithKey(state, key, pageable);
			} else if (!StringUtils.hasText(state) && StringUtils.hasText(user)) {
				pages = this.eventRegisterRepository.findByUserWithKey(user, key, pageable);
			} else {
				pages = this.eventRegisterRepository.getByKey(key, pageable);
			}

		} else {
			if (StringUtils.hasText(state) && StringUtils.hasText(user)) {
				pages = this.eventRegisterRepository.findByStateAndUser(state, user, pageable);
			} else if (StringUtils.hasText(state) && !StringUtils.hasText(user)) {
				pages = this.eventRegisterRepository.initPageByStateId(state, pageable);
			} else if (!StringUtils.hasText(state) && StringUtils.hasText(user)) {
				pages = this.eventRegisterRepository.findByUser(user, pageable);
			} else {
				pages = this.eventRegisterRepository.findAll(pageable);
			}
		}

		return pages.getContent();
	}

	@Override
	public List<EventRegister> getPageByStateAndGrid(int start, int length, String state, String user, String key,
			String orderName, Direction dir) {
		Page<EventRegister> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			if (StringUtils.hasText(state) && StringUtils.hasText(user)) {
				pages = this.eventRegisterRepository.findByStateAndUserGridWithKey(state, user, key, pageable);
			} else if (StringUtils.hasText(state) && !StringUtils.hasText(user)) {
				pages = this.eventRegisterRepository.findByStateWithKey(state, key, pageable);
			} else if (!StringUtils.hasText(state) && StringUtils.hasText(user)) {
				pages = this.eventRegisterRepository.findByUserGridWithKey(user, key, pageable);
			} else {
				pages = this.eventRegisterRepository.getByKey(key, pageable);
			}

		} else {
			if (StringUtils.hasText(state) && StringUtils.hasText(user)) {
				pages = this.eventRegisterRepository.findByStateAndUserGrid(state, user, pageable);
			} else if (StringUtils.hasText(state) && !StringUtils.hasText(user)) {
				pages = this.eventRegisterRepository.initPageByStateId(state, pageable);
			} else if (!StringUtils.hasText(state) && StringUtils.hasText(user)) {
				pages = this.eventRegisterRepository.findByUserGrid(user, pageable);
			} else {
				pages = this.eventRegisterRepository.findAll(pageable);
			}
		}

		return pages.getContent();
	}

	@Override
	public int getCountByStateAndGrid(String state, String user, String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		if (StringUtils.hasText(key)) {
			if (StringUtils.hasText(state) && StringUtils.hasText(user)) {
				return this.eventRegisterRepository.findByStateAndUserGridWithKey(state, user, key, pageable)
						.getContent().size();
			} else if (StringUtils.hasText(state) && !StringUtils.hasText(user)) {
				return this.eventRegisterRepository.findByStateWithKey(state, key, pageable).getContent().size();
			} else if (!StringUtils.hasText(state) && StringUtils.hasText(user)) {
				return this.eventRegisterRepository.findByUserGridWithKey(user, key, pageable).getContent().size();
			} else {
				return this.eventRegisterRepository.getByKey(key, pageable).getContent().size();
			}

		} else {
			if (StringUtils.hasText(state) && StringUtils.hasText(user)) {
				return this.eventRegisterRepository.findByStateAndUserGrid(state, user, pageable).getContent().size();
			} else if (StringUtils.hasText(state) && !StringUtils.hasText(user)) {
				return this.eventRegisterRepository.initPageByStateId(state, pageable).getContent().size();
			} else if (!StringUtils.hasText(state) && StringUtils.hasText(user)) {
				return this.eventRegisterRepository.findByUserGrid(user, pageable).getContent().size();
			} else {
				return this.eventRegisterRepository.findAll(pageable).getSize();
			}
		}
	}

	@Override
	public List<ViewForEventRegisterTask> getPageByTaskStateAndOperate(int start, int length, String state, String user,
			String key, String orderName, Direction dir) {
		Page<ViewForEventRegisterTask> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			if (StringUtils.hasText(state) && StringUtils.hasText(user)) {
				pages = this.viewForEventRegisterTaskRepository.getByStateAndOperateUserWithKey(state, user, key,
						pageable);
			} else if (StringUtils.hasText(state) && !StringUtils.hasText(user)) {
				pages = this.viewForEventRegisterTaskRepository.getByStateWithKey(state, key, pageable);
			} else if (!StringUtils.hasText(state) && StringUtils.hasText(user)) {
				pages = this.viewForEventRegisterTaskRepository.getByOperateUserWithKey(user, key, pageable);
			} else {
				pages = this.viewForEventRegisterTaskRepository.getByKey(key, pageable);
			}

		} else {
			if (StringUtils.hasText(state) && StringUtils.hasText(user)) {
				pages = this.viewForEventRegisterTaskRepository.getByStateAndOperateUser(state, user, pageable);
			} else if (StringUtils.hasText(state) && !StringUtils.hasText(user)) {
				pages = this.viewForEventRegisterTaskRepository.getByState(state, pageable);
			} else if (!StringUtils.hasText(state) && StringUtils.hasText(user)) {
				pages = this.viewForEventRegisterTaskRepository.getByOperateUser(user, pageable);
			} else {
				pages = this.viewForEventRegisterTaskRepository.findAll(pageable);
			}
		}

		return pages.getContent();
	}

	@Override
	public int getCountByTaskStateAndOperateUser(String state, String user, String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		if (StringUtils.hasText(key)) {
			if (StringUtils.hasText(state) && StringUtils.hasText(user)) {
				return this.viewForEventRegisterTaskRepository
						.getByStateAndOperateUserWithKey(state, user, key, pageable).getContent().size();
			} else if (StringUtils.hasText(state) && !StringUtils.hasText(user)) {
				return this.viewForEventRegisterTaskRepository.getByStateWithKey(state, key, pageable).getContent()
						.size();
			} else if (!StringUtils.hasText(state) && StringUtils.hasText(user)) {
				return this.viewForEventRegisterTaskRepository.getByOperateUserWithKey(user, key, pageable).getContent()
						.size();
			} else {
				return this.viewForEventRegisterTaskRepository.getByKey(key, pageable).getContent().size();
			}

		} else {
			if (StringUtils.hasText(state) && StringUtils.hasText(user)) {
				return this.viewForEventRegisterTaskRepository.getByStateAndOperateUser(state, user, pageable)
						.getContent().size();
			} else if (StringUtils.hasText(state) && !StringUtils.hasText(user)) {
				return this.viewForEventRegisterTaskRepository.getByState(state, pageable).getContent().size();
			} else if (!StringUtils.hasText(state) && StringUtils.hasText(user)) {
				return this.viewForEventRegisterTaskRepository.getByOperateUser(user, pageable).getContent().size();
			} else {
				return this.viewForEventRegisterTaskRepository.findAll(pageable).getSize();
			}
		}
	}

	@Override
	public int getCountByStateAndUser(String state, String user, String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		if (StringUtils.hasText(key)) {
			if (StringUtils.hasText(state) && StringUtils.hasText(user)) {
				return this.eventRegisterRepository.findByStateAndUserWithKey(state, user, key, pageable).getContent()
						.size();
			} else if (StringUtils.hasText(state) && !StringUtils.hasText(user)) {
				return this.eventRegisterRepository.findByStateWithKey(state, key, pageable).getContent().size();
			} else if (!StringUtils.hasText(state) && StringUtils.hasText(user)) {
				return this.eventRegisterRepository.findByUserWithKey(user, key, pageable).getContent().size();
			} else {
				return this.eventRegisterRepository.getByKey(key, pageable).getContent().size();
			}

		} else {
			if (StringUtils.hasText(state) && StringUtils.hasText(user)) {
				return this.eventRegisterRepository.findByStateAndUser(state, user, pageable).getContent().size();
			} else if (StringUtils.hasText(state) && !StringUtils.hasText(user)) {
				return this.eventRegisterRepository.initPageByStateId(state, pageable).getContent().size();
			} else if (!StringUtils.hasText(state) && StringUtils.hasText(user)) {
				return this.eventRegisterRepository.findByUser(user, pageable).getContent().size();
			} else {
				return this.eventRegisterRepository.findAll(pageable).getSize();
			}
		}
	}

	@Override
	public int getCountByEventReport(String eventReport, String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		if (!StringUtils.hasText(key))
			return this.eventRegisterRepository.findByEventReportId(eventReport, pageable).getContent().size();

		return this.eventRegisterRepository.findByEventReportIdWithKey(eventReport, key, pageable).getContent().size();
	}

	@Override
	public int getCount(String key) {
		if (!StringUtils.hasText(key))
			return this.eventRegisterRepository.findAll().size();
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		return this.eventRegisterRepository.getByKey(key, pageable).getContent().size();
	}

	@Override
	public List<List<UserSingle>> findUserSingle(String levelId, String eventType, String gridId) {
		Grading grad = this.gradingRepository.findOne(levelId);
		EventType type = this.eventTypeRepository.findOne(eventType);
		GridAreaInfo area = this.gridAreaInfoRepository.findOne(gridId);
		double typeWeigth = 1;
		double areaWeight = 1;
		if (type != null) {
			typeWeigth = (double) type.getEventWeight();
		}
		/*
		 * if (area != null) { areaWeight = (double) area.getGridScale(); }
		 */

		List<List<UserSingle>> list = new LinkedList<>();
		/*
		 * String sql =
		 * "select sl_au_user.id,sl_au_user.`name` as username,sl_au_user.sContactType as userphone,sl_au_user.grading,sl_dcms_grading.gradingName,sl_dcms_grading.gradingScale as scale"
		 * +
		 * ",sl_dcms_option.sExtended1 from sl_au_user left JOIN sl_dcms_grading on sl_dcms_grading.id= sl_au_user.grading "
		 * +
		 * " left join sl_dcms_option on sl_dcms_option.id= sl_dcms_grading.gradingLevel_id where grading is not null and sl_dcms_grading.gradingParent is not null and cast(sl_dcms_option.sExtended1 as SIGNED INTEGER)>="
		 * + grad.getGradingLevel().getExtended1() + "" +
		 * " order by cast(sl_dcms_option.sExtended1 as SIGNED INTEGER) ASC";
		 */
		if (area != null && grad != null) {
			String sql = "select sl_au_user.id,sl_au_user.`name` as username," + "sl_au_user.sContactType as userphone,"
					+ "sl_wcsi_grid_user_details_where.grading,sl_dcms_grading.gradingName,"
					+ "sl_dcms_grading.gradingScale as scale," + "sl_dcms_option.sExtended1 from sl_au_user "
					+ "left join (select * from sl_wcsi_grid_user_details where grid='" + area.getId()
					+ "') as sl_wcsi_grid_user_details_where on sl_au_user.id=sl_wcsi_grid_user_details_where.`user` "
					+ "left JOIN sl_dcms_grading on sl_dcms_grading.id= sl_wcsi_grid_user_details_where.grading  "
					+ "left join sl_dcms_option on sl_dcms_option.id= sl_dcms_grading.gradingLevel_id "
					+ "where sl_wcsi_grid_user_details_where.grading is not null "
					+ "and cast(sl_dcms_option.sExtended1 as SIGNED INTEGER)>=" + grad.getGradingLevel().getExtended1()
					+ " "
					+ "order by cast(sl_dcms_option.sExtended1 as SIGNED INTEGER) ASC,sl_dcms_grading.gradingScale DESC";

			List<Map<String, Object>> arrys = this.jdbcTemplate.queryForList(sql);
			List<UserSingle> users = new LinkedList<>();
			String level = "";
			for (Map<String, Object> arry : arrys) {
				if (!level.equals(arry.get("sExtended1"))) {
					if (users.size() > 0) {
						list.add(users);
					}
					users = new LinkedList<>();
					level = (String) arry.get("sExtended1");
				}
				UserSingle user = new UserSingle();
				user.name = (String) arry.get("username");
				BigDecimal b = new BigDecimal(((float) arry.get("scale")) * typeWeigth * areaWeight);
				double f = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				user.scale = f;
				user.id = (String) arry.get("id");
				user.eventToevaluateId = (String) arry.get("grading");
				user.showtabName = (String) arry.get("gradingName");
				users.add(user);
			}
			if (arrys.size() > 0) {
				list.add(users);
			}
		}
		return list;
	}

	@Override
	public List<List<UserSingle>> findUserSingleByIdentify(String eventRegisterIdentify) {
		List<List<UserSingle>> list = new LinkedList<>();
		String sql = "select a.scale,"
				+ " sl_au_user.`name` as username,sl_au_user.id,sl_dcms_grading.id as grading,sl_dcms_grading.gradingName from "
				+ " (select * from sl_wcsi_eventtoevaluateuser where sl_wcsi_eventtoevaluateuser.registerEvent='"
				+ eventRegisterIdentify + "') as a "
				+ " left join sl_dcms_grading on sl_dcms_grading.id=a.registerEventEvaluateLevel "
				+ " left join sl_au_user on sl_au_user.id=a.registerUser "
				+ " left join sl_dcms_option on sl_dcms_option.id=sl_dcms_grading.gradingLevel_id "
				+ "order by cast(sl_dcms_option.sExtended1 as SIGNED INTEGER) ASC, a.scale DESC";

		List<Map<String, Object>> arrys = this.jdbcTemplate.queryForList(sql);
		List<UserSingle> users = new LinkedList<>();
		String level = "";
		for (Map<String, Object> arry : arrys) {
			if (!level.equals(arry.get("grading"))) {
				if (users.size() > 0) {
					list.add(users);
				}
				users = new LinkedList<>();
				level = (String) arry.get("grading");
			}
			UserSingle user = new UserSingle();
			user.name = (String) arry.get("username");
			BigDecimal b = new BigDecimal((double) arry.get("scale"));
			double f = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			user.scale = (f);
			user.id = (String) arry.get("id");
			user.eventToevaluateId = (String) arry.get("grading");
			user.showtabName = (String) arry.get("gradingName");
			users.add(user);
		}
		if (arrys.size() > 0) {
			list.add(users);
		}
		return list;
	}

	@Override
	public List<EventRegister> initPageByStateId(int pageNow, int pageSize, String optionId) {
		Pageable pageable = new PageRequest(pageNow, pageSize);
		return eventRegisterRepository.initPageByStateId(optionId, pageable).getContent();
	}

	@Override
	public List<EventRegister> initArchievedEventReg(int pageNow, int pageSize, String optionId) {
		Pageable pageable = new PageRequest(pageNow, pageSize);
		return eventRegisterRepository.initPageByStateId(optionId, pageable).getContent();
	}

	@Override
	public void saveAll(EventRegister eventRegister, EventTask eventRegisterTask,
			List<EventToEvaluateUser> eventToEvaluateUser, EventReport eventReport, EventTask eventReportTask) {

		List<EventToEvaluateUser> userList = this.eventToEvaluateUserRepository
				.findListByEventRegister(eventRegister.getId());

		this.eventToEvaluateUserRepository.delete(userList);

		this.eventReportRepository.save(eventReport);
		this.eventRegisterRepository.save(eventRegister);

		this.eventToEvaluateUserRepository.save(eventToEvaluateUser);
		if (eventRegisterTask != null)
			this.eventTaskRepository.save(eventRegisterTask);
		if (eventReportTask != null)
			this.eventTaskRepository.save(eventReportTask);

	}

	@Override
	public String submitEventRegister(String id, User user) {
		String submitStr = "非初始化事件不能发布!";
		EventRegister register = this.eventRegisterRepository.findOne(id);
		if (register != null && register.getRegisterEventState().getTitle().equals("初始化")) {
			List<Option> list = this.optionRepository.findByGroupAndTitle("内部事件流转状态", "已分配");
			if (list.size() > 0) {
				register.setRegisterEventState(list.get(0));
			}
			List<EventTask> taskList = this.eventTaskRepository.getNewestTaskByEventId(register.getId());

			EventTask task = null;
			if (taskList == null || taskList.size() <= 0
					|| !taskList.get(0).getEventTaskState().getTitle().equals("初始化")) {
				return submitStr;
			}

			task = new EventTask();
			task.setEventId(register.getId());
			task.setEventTaskState(register.getRegisterEventState());
			task.setEventTaskTime(DateTime.now().toDate());
			task.setEventTaskUser(user);
			task.setEventTaskOperate("发布操作");
			this.eventRegisterRepository.save(register);
			this.eventTaskRepository.save(task);
			submitStr = "发布成功!";
		}
		return submitStr;
	}

	@Override
	public String backEventRegister(String id, String backMemo, User user) {
		String backStr = "非已分配事件不能收回!";
		EventRegister register = this.eventRegisterRepository.findOne(id);
		if (register != null && register.getRegisterEventState().getTitle().equals("已分配")) {
			List<Option> list = this.optionRepository.findByGroupAndTitle("内部事件流转状态", "初始化");
			if (list.size() > 0) {
				register.setRegisterEventState(list.get(0));
			}
			List<EventTask> taskList = this.eventTaskRepository.getNewestTaskByEventId(register.getId());

			EventTask task = null;
			if (taskList == null || taskList.size() <= 0
					|| !taskList.get(0).getEventTaskState().getTitle().equals("已分配")) {
				return backStr;
			}

			task = new EventTask();
			task.setEventId(register.getId());
			task.setEventTaskMemo(backMemo);
			task.setEventTaskState(register.getRegisterEventState());
			task.setEventTaskTime(DateTime.now().toDate());
			task.setEventTaskUser(user);
			task.setEventTaskOperate("收回操作");
			this.eventRegisterRepository.save(register);
			this.eventTaskRepository.save(task);
			backStr = "收回成功!";
		}
		return backStr;
	}

	@Override
	public String rejectEventRegister(String id, String rejectMemo, User user, String photoUrl) {
		String rejectStr = "非已分配事件不能退回!";
		EventRegister register = this.eventRegisterRepository.findOne(id);
		if (register != null && register.getRegisterEventState().getTitle().equals("已分配")) {
			List<Option> list = this.optionRepository.findByGroupAndTitle("内部事件流转状态", "已退回");
			if (list.size() > 0) {
				register.setRegisterEventState(list.get(0));
			}
			List<EventTask> taskList = this.eventTaskRepository.getNewestTaskByEventId(register.getId());

			EventTask task = null;
			if (taskList == null || taskList.size() <= 0
					|| !taskList.get(0).getEventTaskState().getTitle().equals("已分配")) {
				return rejectStr;
			}

			task = new EventTask();
			task.setEventId(register.getId());
			task.setEventTaskMemo(rejectMemo);
			task.setEventTaskState(register.getRegisterEventState());
			task.setEventTaskTime(DateTime.now().toDate());
			task.setEventTaskUser(user);
			photoUrl = ("".equals(photoUrl) ? null : photoUrl);
			task.setPhotoUrl(photoUrl);
			task.setEventTaskOperate("退回操作");
			this.eventRegisterRepository.save(register);
			this.eventTaskRepository.save(task);
			rejectStr = "退回成功!";
		}
		return rejectStr;
	}

	@Override
	public String rejectBackEventRegister(String id, String rejectBackMemo, User user) {
		String rejectBackStr = "非退回事件不能确认退回!";
		EventRegister register = this.eventRegisterRepository.findOne(id);
		if (register != null && register.getRegisterEventState().getTitle().equals("已退回")) {
			List<Option> list = this.optionRepository.findByGroupAndTitle("内部事件流转状态", "初始化");
			if (list.size() > 0) {
				register.setRegisterEventState(list.get(0));
			}
			List<EventTask> taskList = this.eventTaskRepository.getNewestTaskByEventId(register.getId());

			EventTask task = null;
			if (taskList == null || taskList.size() <= 0
					|| !taskList.get(0).getEventTaskState().getTitle().equals("已退回")) {
				return rejectBackStr;
			}

			task = new EventTask();
			task.setEventId(register.getId());
			task.setEventTaskMemo(rejectBackMemo);
			task.setEventTaskState(register.getRegisterEventState());
			task.setEventTaskTime(DateTime.now().toDate());
			task.setEventTaskUser(user);
			task.setEventTaskOperate("确认退回操作");
			this.eventRegisterRepository.save(register);
			this.eventTaskRepository.save(task);
			rejectBackStr = "确认退回成功!";
		}
		return rejectBackStr;
	}

	@Override
	public String reveiveEventRegister(String id, User user) {
		String receiveStr = "非已分配事件不能接收!";
		EventRegister register = this.eventRegisterRepository.findOne(id);
		if (register != null && register.getRegisterEventState().getTitle().equals("已分配")) {
			List<Option> list = this.optionRepository.findByGroupAndTitle("内部事件流转状态", "处理中");
			if (list.size() > 0) {
				register.setRegisterEventState(list.get(0));
			}
			List<EventTask> taskList = this.eventTaskRepository.getNewestTaskByEventId(register.getId());

			EventTask task = null;
			if (taskList == null || taskList.size() <= 0
					|| !taskList.get(0).getEventTaskState().getTitle().equals("已分配")) {
				return receiveStr;
			}

			task = new EventTask();
			task.setEventId(register.getId());
			task.setEventTaskState(register.getRegisterEventState());
			task.setEventTaskTime(DateTime.now().toDate());
			task.setEventTaskUser(user);
			task.setEventTaskOperate("接收操作");
			this.eventRegisterRepository.save(register);
			this.eventTaskRepository.save(task);
			receiveStr = "接收成功!";
		}
		return receiveStr;
	}

	@Override
	public EventTask getRejectBackReason(String id) {

		List<EventTask> taskList = this.eventTaskRepository.getNewestTaskByEventId(id);
		if (taskList == null || taskList.size() <= 0 || !taskList.get(0).getEventTaskState().getTitle().equals("已退回")) {
			return new EventTask();
		} else {
			return taskList.get(0);
		}
	}

	@Override
	public List<EventRegister> findByReportId(String reportId) {

		return eventRegisterRepository.findByReportId(reportId);
	}

	@Override
	public List<EventRegister> getRegistersPageByReportId(String reportId, int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return eventRegisterRepository.getRegistersPageByReportId(reportId, pageable).getContent();
	}

	@Override
	public int getRegistersCountByReportId(String reportId) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return eventRegisterRepository.getRegistersPageByReportId(reportId, pageable).getContent().size();
	}

	/***********/
	@Override
	public List<EventRegister> getPageByEventReportAndType(int start, int length, String eventReport, String key,
			String type, String orderName, Direction dir) {
		Page<EventRegister> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.eventRegisterRepository.findByEventReportIdWithKeyAndType(eventReport, key, type, pageable);
		} else {
			pages = this.eventRegisterRepository.findByEventReportIdAndType(eventReport, type, pageable);
		}

		return pages.getContent();
	}

	@Override
	public int getCountByEventReportAndType(String eventReport, String key, String type) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		if (!StringUtils.hasText(key))
			return this.eventRegisterRepository.findByEventReportIdAndType(eventReport, type, pageable).getContent()
					.size();

		return this.eventRegisterRepository.findByEventReportIdWithKeyAndType(eventReport, key, type, pageable)
				.getContent().size();
	}

	@Override
	public List<EventRegister> getPageAndType(int start, int length, String key, String type, String orderName,
			Direction dir) {
		Page<EventRegister> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.eventRegisterRepository.getByKeyAndType(key, type, pageable);
		} else {
			pages = this.eventRegisterRepository.getByType(type, pageable);
		}

		return pages.getContent();
	}

	@Override
	public int getCountAndType(String key, String type) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);

		if (!StringUtils.hasText(key))
			return this.eventRegisterRepository.getByType(type, pageable).getContent().size();

		return this.eventRegisterRepository.getByKeyAndType(key, type, pageable).getContent().size();
	}

	@Override
	public List<EventRegister> getRegisterUserAndType(int start, int length, String userId, String type,
			String orderName, Direction dir) {
		Page<EventRegister> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		pages = this.eventRegisterRepository.getRegisterUserAndType(userId, type, pageable);
		return pages.getContent();
	}

	@Override
	public List<EventRegisterAndTaskView> findPageByReportAndType(int start, int length, String eventReport, String key,
			String type, String orderName, Direction dir) {
		Page<EventRegisterAndTaskView> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.eventRegisterRepository.findPageByReportWithKeyAndType(eventReport, key, type, pageable);
		} else {
			pages = this.eventRegisterRepository.findPageByReportAndType(eventReport, type, pageable);
		}

		return pages.getContent();
	}

	@Override
	public List<EventRegisterTask> findPageByReportAndType2(int start, int length, String eventReport, String key,
			String type, String orderName, Direction dir) {
		String SQL1 = "select " + 
				"	a.id as 'id'," + 
				"	a.registerEventName as 'registerEventName'," + 
				"	a.registerEventContent as 'registerEventContent'," + 
				"	a.registerEventReportTime as 'registerEventReportTime'," + 
				"	a.registerEventIdentify as 'registerEventIdentify'," + 
				"	e.eventName as 'registerEventType'," + 
				"	gd.gradingName as 'registerEventEvaluateLevel'," + 
				"	l.sTitle as 'registerEventSeriousLevel'," + 
				"	a.registerEventImgAdress as 'registerEventImgAdress'," + 
				"	a.registerEventAdress as 'registerEventAdress'," + 
				"	g.gridName as 'registerEventToNetGrid'," + 
				"	s.sTitle as 'registerEventState'," + 
				"	b.diffTime as 'diffTime'," + 
				"	a.acceptLimitSpan as 'acceptLimitSpan'," + 
				"	a.dealLimitSpan as 'dealLimitSpan'" + 
				" from sl_wcsi_eventregister a "+
				" LEFT join sl_dcms_option l" + 
				"	on a.registerEventSeriousLevel = l.id " + 
				" LEFT join sl_dcms_option s" + 
				"	on a.registerEventState = s.id " + 
				" left join sl_wcsi_gridArea g" + 
				"	on a.registerEventToNetGrid = g.id" + 
				" left join sl_dcms_grading gd" + 
				"	on a.registerEventEvaluateLevel = gd.id" + 
				" left join sl_dcms_eventType e" + 
				"	on a.registerEventType = e.id ," + 
				"	ViewForTimeOutTask b," + 
				"	sl_dcms_option c" 
				+" where b.unfinished = '1' and a.id = b.eventId and s.id = b.eventTaskState "
				+"and a.registerEventState=c.id";
		if(StringUtils.hasText(eventReport)) {
			SQL1+= " and a.registerEventToReportEvent = '"+ eventReport +"'";
		}
		
		if(StringUtils.hasText(type))
		{
			SQL1+=" and c.sTitle='"+type+"'";
		}
		if(StringUtils.hasText(key)){
			SQL1+=" and (a.registerEventName like '%"+key+"%' or a.registerEventContent like '%"+key+"%' or c.sTitle like '%"+key+"%')";	
		}
		SQL1 += " order by a." + orderName + " desc ";
		SQL1 += " limit "+ (int)(Math.floor(start / length) * length) + " , " +  (int)((Math.floor(start / length) + 1) * length);
		
		List<Map<String, Object>> list1 = this.jdbcTemplate.queryForList(SQL1);
		
		List<EventRegisterTask> result1 = new ArrayList<>();
		for(int i=0;i<list1.size();i++) {
			Map<String, Object> map = list1.get(i);
			EventRegisterTask a = new EventRegisterTask();
			a.setId((String) map.get("id"));
			a.setRegisterEventName((String) map.get("registerEventName"));
			a.setRegisterEventReportTime((Date) map.get("registerEventReportTime"));
			a.setRegisterEventIdentify((String) map.get("registerEventIdentify"));
			a.setRegisterEventType((String) map.get("registerEventType"));
			a.setRegisterEventEvaluateLevel((String) map.get("registerEventEvaluateLevel"));
			a.setRegisterEventSeriousLevel((String) map.get("registerEventSeriousLevel"));
			a.setRegisterEventImgAdress((String) map.get("registerEventImgAdress"));
			a.setRegisterEventAdress((String) map.get("registerEventAdress"));
			a.setRegisterEventToNetGrid((String) map.get("registerEventToNetGrid"));
			a.setRegisterEventState((String) map.get("registerEventState"));
			a.setDiffTimes(Double.parseDouble(map.get("diffTime").toString().trim()));
			a.setAcceptLimitSpan((double) map.get("acceptLimitSpan"));
			a.setDealLimitSpan((double) map.get("dealLimitSpan"));
			result1.add(a);
		}
		
		return result1;
	}

	@Override
	public List<EventRegisterTask> findPageByReportAndType3(int start, int length,  String roleId, String eventReport, String key,
			String type, String orderName, Direction dir) {
		String SQL1 = "select " + 
				"	a.id as 'id'," + 
				"	a.createTime as 'createTime'," +
				" 	a.registerEventType as 'eventTypeId'," +
				"	a.registerEventName as 'registerEventName'," + 
				"	a.registerEventContent as 'registerEventContent'," + 
				"	a.registerEventReportTime as 'registerEventReportTime'," + 
				"	a.registerEventIdentify as 'registerEventIdentify'," + 
				"	e.eventName as 'registerEventType'," + 
				"	gd.gradingName as 'registerEventEvaluateLevel'," + 
				"	l.sTitle as 'registerEventSeriousLevel'," + 
				"	a.registerEventImgAdress as 'registerEventImgAdress'," + 
				"	a.registerEventAdress as 'registerEventAdress'," + 
				"	g.gridName as 'registerEventToNetGrid'," + 
				"	s.sTitle as 'registerEventState'," + 
				"	b.diffTime as 'diffTime'," + 
				"	a.registerEventToReportEvent as 'registerEventToReportEvent'," + 
				"	a.acceptLimitSpan as 'acceptLimitSpan'," + 
				"	a.dealLimitSpan as 'dealLimitSpan'" + 
				" from sl_wcsi_eventregister a "+
				" LEFT join sl_dcms_option l" + 
				"	on a.registerEventSeriousLevel = l.id " + 
				" LEFT join sl_dcms_option s" + 
				"	on a.registerEventState = s.id " + 
				" left join sl_wcsi_gridArea g" + 
				"	on a.registerEventToNetGrid = g.id" + 
				" left join sl_dcms_grading gd" + 
				"	on a.registerEventEvaluateLevel = gd.id" + 
				" left join sl_dcms_eventType e" + 
				"	on a.registerEventType = e.id ," + 
				"	ViewForTimeOutTask b," + 
				"	sl_dcms_option c" 
				+" where b.unfinished = '1' and a.id = b.eventId and s.id = b.eventTaskState "
				+"and a.registerEventState=c.id";
		if(StringUtils.hasText(eventReport)) {
			SQL1+= " and a.registerEventToReportEvent = '"+ eventReport +"'";
		}
		
		if(StringUtils.hasText(type))
		{
			SQL1+=" and c.sTitle='"+type+"'";
		}
		if(StringUtils.hasText(key)){
			SQL1+=" and (a.registerEventName like '%"+key+"%' or a.registerEventContent like '%"+key+"%' or c.sTitle like '%"+key+"%')";	
		}
		
		String SQL = " select sq.* from (select e2r.* from sl_wcsi_e2r e2r where e2r.roleId = '"+ roleId +"' ) er left join ( " + SQL1 + " ) sq on er.eventTypeId = sq.eventTypeId  where sq.id is not null ";
		
		SQL += " order by sq." + orderName + " desc ";
		
		SQL1 += " limit "+ (int)(Math.floor(start / length) * length) + " , " +  (int)((Math.floor(start / length) + 1) * length);
		
		List<Map<String, Object>> list1 = this.jdbcTemplate.queryForList(SQL1);
		
		List<EventRegisterTask> result1 = new ArrayList<>();
		for(int i=0;i<list1.size();i++) {
			Map<String, Object> map = list1.get(i);
			EventRegisterTask a = new EventRegisterTask();
			a.setId((String) map.get("id"));
			a.setRegisterEventName((String) map.get("registerEventName"));
			a.setRegisterEventReportTime((Date) map.get("registerEventReportTime"));
			a.setRegisterEventIdentify((String) map.get("registerEventIdentify"));
			a.setRegisterEventType((String) map.get("registerEventType"));
			a.setRegisterEventEvaluateLevel((String) map.get("registerEventEvaluateLevel"));
			a.setRegisterEventSeriousLevel((String) map.get("registerEventSeriousLevel"));
			a.setRegisterEventImgAdress((String) map.get("registerEventImgAdress"));
			a.setRegisterEventAdress((String) map.get("registerEventAdress"));
			a.setRegisterEventToNetGrid((String) map.get("registerEventToNetGrid"));
			a.setRegisterEventState((String) map.get("registerEventState"));
			a.setDiffTimes(Double.parseDouble(map.get("diffTime").toString().trim()));
			a.setAcceptLimitSpan((double) map.get("acceptLimitSpan"));
			a.setDealLimitSpan((double) map.get("dealLimitSpan"));
			a.setRegisterEventToReportEvent((String) map.get("registerEventToReportEvent"));
			result1.add(a);
		}
		
		return result1;
	}

	@Override
	public int findCountByReportAndType3(String roleId, String eventReport, String key, String type) {
		String SQL1 = "select " + 
				"	a.id as 'id'," + 
				"	a.createTime as 'createTime'," +
				" 	a.registerEventType as 'eventTypeId'," +
				"	a.registerEventName as 'registerEventName'," + 
				"	a.registerEventContent as 'registerEventContent'," + 
				"	a.registerEventReportTime as 'registerEventReportTime'," + 
				"	a.registerEventIdentify as 'registerEventIdentify'," + 
				"	e.eventName as 'registerEventType'," + 
				"	gd.gradingName as 'registerEventEvaluateLevel'," + 
				"	l.sTitle as 'registerEventSeriousLevel'," + 
				"	a.registerEventImgAdress as 'registerEventImgAdress'," + 
				"	a.registerEventAdress as 'registerEventAdress'," + 
				"	g.gridName as 'registerEventToNetGrid'," + 
				"	s.sTitle as 'registerEventState'," + 
				"	b.diffTime as 'diffTime'," + 
				"	a.acceptLimitSpan as 'acceptLimitSpan'," + 
				"	a.dealLimitSpan as 'dealLimitSpan'" + 
				" from sl_wcsi_eventregister a "+
				" LEFT join sl_dcms_option l" + 
				"	on a.registerEventSeriousLevel = l.id " + 
				" LEFT join sl_dcms_option s" + 
				"	on a.registerEventState = s.id " + 
				" left join sl_wcsi_gridArea g" + 
				"	on a.registerEventToNetGrid = g.id" + 
				" left join sl_dcms_grading gd" + 
				"	on a.registerEventEvaluateLevel = gd.id" + 
				" left join sl_dcms_eventType e" + 
				"	on a.registerEventType = e.id ," + 
				"	ViewForTimeOutTask b," + 
				"	sl_dcms_option c" 
				+" where b.unfinished = '1' and a.id = b.eventId and s.id = b.eventTaskState "
				+"and a.registerEventState=c.id";
		if(StringUtils.hasText(eventReport)) {
			SQL1+= " and a.registerEventToReportEvent = '"+ eventReport +"'";
		}
		
		if(StringUtils.hasText(type))
		{
			SQL1+=" and c.sTitle='"+type+"'";
		}
		if(StringUtils.hasText(key)){
			SQL1+=" and (a.registerEventName like '%"+key+"%' or a.registerEventContent like '%"+key+"%' or c.sTitle like '%"+key+"%')";	
		}
		
		String SQL = " select count(1) as num from (select e2r.* from sl_wcsi_e2r e2r where e2r.roleId = '"+ roleId +"' ) er left join ( " + SQL1 + " ) sq on er.eventTypeId = sq.eventTypeId  where sq.id is not null ";
		
		int score = 0;
		score = this.jdbcTemplate.queryForObject(SQL,Integer.class);
		return score;
	}

	@Override
	public int findCountByReportAndType(String eventReport, String key, String type) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		if (!StringUtils.hasText(key))
			return this.eventRegisterRepository.findPageByReportAndType(eventReport, type, pageable).getContent()
					.size();
		
		return this.eventRegisterRepository.findPageByReportWithKeyAndType(eventReport, key, type, pageable)
				.getContent().size();
	}

	@Override
	public List<EventRegisterAndTaskView> findPageAndType(int start, int length, String key, String type, String string,
			Direction desc) {
		Page<EventRegisterAndTaskView> pages = null;
		Pageable pageable = new PageRequest(start / length, length, desc, string);
		if (StringUtils.hasText(key)) {
			pages = this.eventRegisterRepository.findByKeyAndType(key, type, pageable);
		} else {
			pages = this.eventRegisterRepository.findByType(type, pageable);
		}

		return pages.getContent();
	}
	
	@Override
	public List<EventRegisterTask> findPageAndType2(int start, int length, String key, String type, String string,
			Direction desc) {
		
		String SQL1 = "select " + 
				"	a.id as 'id'," + 
				"	a.registerEventName as 'registerEventName'," + 
				"	a.registerEventContent as 'registerEventContent'," + 
				"	a.registerEventReportTime as 'registerEventReportTime'," + 
				"	a.registerEventIdentify as 'registerEventIdentify'," + 
				"	e.eventName as 'registerEventType'," + 
				"	gd.gradingName as 'registerEventEvaluateLevel'," + 
				"	l.sTitle as 'registerEventSeriousLevel'," + 
				"	a.registerEventImgAdress as 'registerEventImgAdress'," + 
				"	a.registerEventAdress as 'registerEventAdress'," + 
				"	g.gridName as 'registerEventToNetGrid'," + 
				"	s.sTitle as 'registerEventState'," + 
				"	b.diffTime as 'diffTime'," + 
				"	a.acceptLimitSpan as 'acceptLimitSpan'," + 
				"	a.dealLimitSpan as 'dealLimitSpan'" + 
				" from sl_wcsi_eventregister a "+
				" LEFT join sl_dcms_option l" + 
				"	on a.registerEventSeriousLevel = l.id " + 
				" LEFT join sl_dcms_option s" + 
				"	on a.registerEventState = s.id " + 
				" left join sl_wcsi_gridArea g" + 
				"	on a.registerEventToNetGrid = g.id" + 
				" left join sl_dcms_grading gd" + 
				"	on a.registerEventEvaluateLevel = gd.id" + 
				" left join sl_dcms_eventType e" + 
				"	on a.registerEventType = e.id ," + 
				"	ViewForTimeOutTask b," + 
				"	sl_dcms_option c" 
				+" where b.unfinished = '1' and a.id = b.eventId and s.id = b.eventTaskState "
				+"and a.registerEventState=c.id";
		if(StringUtils.hasText(type))
		{
			SQL1+=" and c.sTitle='"+type+"'";
		}
		if(StringUtils.hasText(key)){
			SQL1+=" and (a.registerEventName like '%"+key+"%' or a.registerEventContent like '%"+key+"%' or c.sTitle like '%"+key+"%')";	
		}
		SQL1 += " order by a." + string + " desc ";
		SQL1 += " limit "+ (int)(Math.floor(start / length) * length) + " , " +  (int)((Math.floor(start / length) + 1) * length);
		
		List<Map<String, Object>> list1 = this.jdbcTemplate.queryForList(SQL1);
		
		List<EventRegisterTask> result1 = new ArrayList<>();
		for(int i=0;i<list1.size();i++) {
			Map<String, Object> map = list1.get(i);
			EventRegisterTask a = new EventRegisterTask();
			a.setId((String) map.get("id"));
			a.setRegisterEventName((String) map.get("registerEventName"));
			a.setRegisterEventReportTime((Date) map.get("registerEventReportTime"));
			a.setRegisterEventIdentify((String) map.get("registerEventIdentify"));
			a.setRegisterEventType((String) map.get("registerEventType"));
			a.setRegisterEventEvaluateLevel((String) map.get("registerEventEvaluateLevel"));
			a.setRegisterEventSeriousLevel((String) map.get("registerEventSeriousLevel"));
			a.setRegisterEventImgAdress((String) map.get("registerEventImgAdress"));
			a.setRegisterEventAdress((String) map.get("registerEventAdress"));
			a.setRegisterEventToNetGrid((String) map.get("registerEventToNetGrid"));
			a.setRegisterEventState((String) map.get("registerEventState"));
			a.setDiffTimes(Double.parseDouble(map.get("diffTime").toString().trim()));
			a.setAcceptLimitSpan((double) map.get("acceptLimitSpan"));
			a.setDealLimitSpan((double) map.get("dealLimitSpan"));
			result1.add(a);
		}
		
		return result1;
	}

	@Override
	public List<EventRegisterTask> findPageAndType3(int start, int length, String roleId, String key, String type, String string,
			Direction desc) {
		
		String SQL1 = "select " + 
				"	a.id as 'id'," + 
				"	a.createTime as 'createTime'," +
				" 	a.registerEventType as 'eventTypeId'," +
				"	a.registerEventName as 'registerEventName'," + 
				"	a.registerEventContent as 'registerEventContent'," + 
				"	a.registerEventReportTime as 'registerEventReportTime'," + 
				"	a.registerEventIdentify as 'registerEventIdentify'," + 
				"	e.eventName as 'registerEventType'," + 
				"	gd.gradingName as 'registerEventEvaluateLevel'," + 
				"	l.sTitle as 'registerEventSeriousLevel'," + 
				"	a.registerEventImgAdress as 'registerEventImgAdress'," + 
				"	a.registerEventAdress as 'registerEventAdress'," + 
				"	g.gridName as 'registerEventToNetGrid'," + 
				"	s.sTitle as 'registerEventState'," + 
				"	b.diffTime as 'diffTime'," + 
				"	a.registerEventToReportEvent as 'registerEventToReportEvent'," + 
				"	a.acceptLimitSpan as 'acceptLimitSpan'," + 
				"	a.dealLimitSpan as 'dealLimitSpan'" + 
				" from sl_wcsi_eventregister a "+
				" LEFT join sl_dcms_option l" + 
				"	on a.registerEventSeriousLevel = l.id " + 
				" LEFT join sl_dcms_option s" + 
				"	on a.registerEventState = s.id " + 
				" left join sl_wcsi_gridArea g" + 
				"	on a.registerEventToNetGrid = g.id" + 
				" left join sl_dcms_grading gd" + 
				"	on a.registerEventEvaluateLevel = gd.id" + 
				" left join sl_dcms_eventType e" + 
				"	on a.registerEventType = e.id ," + 
				"	ViewForTimeOutTask b," + 
				"	sl_dcms_option c" 
				+" where b.unfinished = '1' and a.id = b.eventId and s.id = b.eventTaskState "
				+"and a.registerEventState=c.id";
		if(StringUtils.hasText(type))
		{
			SQL1+=" and c.sTitle='"+type+"'";
		}
		if(StringUtils.hasText(key)){
			SQL1+=" and (a.registerEventName like '%"+key+"%' or a.registerEventContent like '%"+key+"%' or c.sTitle like '%"+key+"%')";	
		}
		
		String SQL = " select sq.* from (select e2r.* from sl_wcsi_e2r e2r where e2r.roleId = '"+ roleId +"' ) er left join ( " + SQL1 + " ) sq on er.eventTypeId = sq.eventTypeId where sq.id is not null ";
		
		SQL += " order by sq." + string + " desc ";
		SQL += " limit "+ (int)(Math.floor(start / length) * length) + " , " +  (int)((Math.floor(start / length) + 1) * length);
		
		List<Map<String, Object>> list1 = this.jdbcTemplate.queryForList(SQL);
		
		List<EventRegisterTask> result1 = new ArrayList<>();
		for(int i=0;i<list1.size();i++) {
			Map<String, Object> map = list1.get(i);
			EventRegisterTask a = new EventRegisterTask();
			a.setId((String) map.get("id"));
			a.setRegisterEventName((String) map.get("registerEventName"));
			a.setRegisterEventReportTime((Date) map.get("registerEventReportTime"));
			a.setRegisterEventIdentify((String) map.get("registerEventIdentify"));
			a.setRegisterEventType((String) map.get("registerEventType"));
			a.setRegisterEventEvaluateLevel((String) map.get("registerEventEvaluateLevel"));
			a.setRegisterEventSeriousLevel((String) map.get("registerEventSeriousLevel"));
			a.setRegisterEventImgAdress((String) map.get("registerEventImgAdress"));
			a.setRegisterEventAdress((String) map.get("registerEventAdress"));
			a.setRegisterEventToNetGrid((String) map.get("registerEventToNetGrid"));
			a.setRegisterEventState((String) map.get("registerEventState"));
			a.setDiffTimes(Double.parseDouble(map.get("diffTime").toString().trim()));
			a.setAcceptLimitSpan((double) map.get("acceptLimitSpan"));
			a.setDealLimitSpan((double) map.get("dealLimitSpan"));
			a.setRegisterEventToReportEvent((String) map.get("registerEventToReportEvent"));
			result1.add(a);
		}
		
		return result1;
	}
	
	@Override
	public int findCountAndType3(String roleId,String key, String type) {
		
		
		String SQL1 = "select " + 
				"	a.id as 'id'," + 
				"	a.createTime as 'createTime'," +
				" 	a.registerEventType as 'eventTypeId'," +
				"	a.registerEventName as 'registerEventName'," + 
				"	a.registerEventContent as 'registerEventContent'," + 
				"	a.registerEventReportTime as 'registerEventReportTime'," + 
				"	a.registerEventIdentify as 'registerEventIdentify'," + 
				"	e.eventName as 'registerEventType'," + 
				"	gd.gradingName as 'registerEventEvaluateLevel'," + 
				"	l.sTitle as 'registerEventSeriousLevel'," + 
				"	a.registerEventImgAdress as 'registerEventImgAdress'," + 
				"	a.registerEventAdress as 'registerEventAdress'," + 
				"	g.gridName as 'registerEventToNetGrid'," + 
				"	s.sTitle as 'registerEventState'," + 
				"	b.diffTime as 'diffTime'," + 
				"	a.acceptLimitSpan as 'acceptLimitSpan'," + 
				"	a.dealLimitSpan as 'dealLimitSpan'" + 
				" from sl_wcsi_eventregister a "+
				" LEFT join sl_dcms_option l" + 
				"	on a.registerEventSeriousLevel = l.id " + 
				" LEFT join sl_dcms_option s" + 
				"	on a.registerEventState = s.id " + 
				" left join sl_wcsi_gridArea g" + 
				"	on a.registerEventToNetGrid = g.id" + 
				" left join sl_dcms_grading gd" + 
				"	on a.registerEventEvaluateLevel = gd.id" + 
				" left join sl_dcms_eventType e" + 
				"	on a.registerEventType = e.id ," + 
				"	ViewForTimeOutTask b," + 
				"	sl_dcms_option c" 
				+" where b.unfinished = '1' and a.id = b.eventId and s.id = b.eventTaskState "
				+"and a.registerEventState=c.id";
		if(StringUtils.hasText(type))
		{
			SQL1+=" and c.sTitle='"+type+"'";
		}
		if(StringUtils.hasText(key)){
			SQL1+=" and (a.registerEventName like '%"+key+"%' or a.registerEventContent like '%"+key+"%' or c.sTitle like '%"+key+"%')";	
		}
		
		String SQL = " select count(1) as num from (select e2r.* from sl_wcsi_e2r e2r where e2r.roleId = '"+ roleId +"' ) er left join ( " + SQL1 + " ) sq on er.eventTypeId = sq.eventTypeId where sq.id is not null ";
		
		int score = 0;
		score = this.jdbcTemplate.queryForObject(SQL,Integer.class);
		return score;
	}
	
	
    private int getRegisterCount(String key,String type){
	    	String SQL = "select count(*) from sl_wcsi_eventregister a, ViewForTimeOutTask b,sl_dcms_option c "
	    	        +"where b.unfinished = '1' and a.id = b.eventId and a.registerEventState = b.eventTaskState "
	    			+"and a.registerEventState=c.id";
	    	
					//+ " where a.registerUser = :userId and a.createTime >:begin and a.createTime <:end ";
	    	if(StringUtils.hasText(type))
	    	{
	    		SQL+=" and c.sTitle='"+type+"'";
	    	}
		if(StringUtils.hasText(key)){
			SQL+=" and (a.registerEventName like '%"+key+"%' or a.registerEventContent like '%"+key+"%' or c.sTitle like '%"+key+"%')";	
		}
		int score = 0;
		score = this.jdbcTemplate.queryForObject(SQL,Integer.class);
		return score;
    }
    @Override
    public int findCountAndType(String key, String type) {
	    	return getRegisterCount(key,type);
	    	/*Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
			if (!StringUtils.hasText(key))
				return this.eventRegisterRepository.findByType(type, pageable).getContent().size();
	
			return this.eventRegisterRepository.findByKeyAndType(key, type, pageable).getContent().size();*/
	    }

	@Override
	public List<EventRegisterAndTaskView> findUnderTimeoutEventByKey(int start, int length, String key, Option type) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return eventRegisterRepository.findUnderTimeoutEventByKey(pageable, key, type).getContent();
	}

	@Override
	public int findCountUnderTimeoutEventByKey(String key, Option type) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return eventRegisterRepository.findUnderTimeoutEventByKey(pageable, key, type).getContent().size();
	}

	@Override
	public List<EventRegisterAndTaskView> findUnderTimeoutEvent(int start, int length, Option type) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return eventRegisterRepository.findUnderTimeoutEvent(pageable, type).getContent();
	}

	@Override
	public int findCountUnderTimeoutEvent(Option type) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return eventRegisterRepository.findUnderTimeoutEvent(pageable, type).getContent().size();
	}

	@Override
	public List<EventRegisterAndTaskView> findUnderTimeoutEventNew(int start, int length, String key, double limit, Option type) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		List<EventRegisterAndTaskView> pages = null;
		if(StringUtils.hasText(key) ){
			pages = eventRegisterRepository.findUnderTimeoutEventByKey(key, type, limit, pageable).getContent();
		}else {
			pages = eventRegisterRepository.findUnderTimeoutEvent(type, limit, pageable).getContent();
		}
		return pages;
	}

	@Override
	public int findCountUnderTimeoutEventNew(String key, double limit, Option type) {
		int count = 0;
		if(StringUtils.hasText(key) ){
			count = eventRegisterRepository.getCountUnderTimeoutEventByKey(key, type, limit);
		}else {
			count = eventRegisterRepository.getCountUnderTimeoutEvent(type, limit);
		}
		return count;
	}

	@Override
	public EventTask findTask(String registerId, Option registerEventState) {
		
		return eventRegisterRepository.findTask(registerId,registerEventState);
	}
	
	
	@Override
	public List<Map<String, Object>> getPageByTaskStateAndOperateAndRole(int start, int length, String roleId, String state, String user,String key, String orderName, String dir){
		
		//  select a from ViewForEventRegisterTask a where a.eventTaskState=?1 and a.registerEventState=?1 and a.eventTaskUser=?2 and (a.registerEventContent like %?3%)
		
		String SQL1 = "select  a.* "
				+ " from ViewForEventRegisterTask a "
				+ " where 1 = 1 ";
		
		if(StringUtils.hasText(state)){
			SQL1+=" and a.eventTaskState='"+state+"' and a.registerEventState='"+state+"'";
		}

		if(StringUtils.hasText(user)){
			SQL1+=" and a.eventTaskUser='"+user+"' ";
		}
		
		if(StringUtils.hasText(key)){
			SQL1+=" and (a.registerEventContent like '%"+key+"%') ";	
		}
		
		
		String SQL = " select sq.* from (select e2r.* from sl_wcsi_e2r e2r where e2r.roleId = '"+ roleId +"' ) er left join ( " + SQL1 + " ) sq on er.eventTypeId = sq.registerEventType where sq.id is not null  ";
		
		SQL += " order by sq." + orderName + " " + ( StringUtils.hasText(dir) ? dir : "" );
		SQL += " limit "+ (int)(Math.floor(start / length) * length) + " , " +  (int)((Math.floor(start / length) + 1) * length);
		
		List<Map<String, Object>> resultList = this.jdbcTemplate.queryForList(SQL);
		
		return resultList;
	}
	
	@Override
	public int getCountByTaskStateAndOperateUserAndRole(String roleId,String state, String user, String key) {
		String SQL1 = "select  a.* "
				+ " from ViewForEventRegisterTask a "
				+ " where 1 = 1 ";
		
		if(StringUtils.hasText(state)){
			SQL1+=" and a.eventTaskState='"+state+"' and a.registerEventState='"+state+"'";
		}

		if(StringUtils.hasText(user)){
			SQL1+=" and a.eventTaskUser='"+user+"' ";
		}
		
		if(StringUtils.hasText(key)){
			SQL1+=" and (a.registerEventContent like '%"+key+"%') ";	
		}
		
		String SQL = " select count(1) from (select e2r.* from sl_wcsi_e2r e2r where e2r.roleId = '"+ roleId +"' ) er left join ( " + SQL1 + " ) sq on er.eventTypeId = sq.registerEventType where sq.id is not null ";
		
		int count = 0;
		count = this.jdbcTemplate.queryForObject(SQL, Integer.class);
		
		return count;
	}

	@Override
	public List<Map<String, Object>> findUnderTimeoutEventByRole(int start, int length, String roleId, String key, double limit, Option type, String orderName, String dir) {

		//@Query("select distinct new com.sl.au.entity.EventRegisterAndTaskView(a,b) from EventRegister a, ViewForTimeOutTask b where b.unfinished = '1' and a.id=b.eventId and a.registerEventState=b.eventTaskState and a.registerEventState=?2 and (a.dealLimitSpan - b.diffTime) > 0 and (a.dealLimitSpan - b.diffTime) <= ?3 and (a.registerEventName like %?1% or a.registerEventContent like %?1% or a.registerEventState.title like %?1%)")
		
		String SQL1 = "select b.*, "
				+ " a.eventTaskUserName, "
				+ " a.diffTime, "
				+ " et.eventName as 'eventTypeName', "
				+ " ga.gridName as 'gridName', "
				+ " o.sTitle as 'registerEventStateTitle', "
				+ " o.sTitle as 'registerEventSeriousLevelTitle', "
				+ " gr.gradingName as 'gradingName' "
				//+ " d.divisionName as 'divisionName' "
				+ " from ViewForTimeOutTask a "
				+ " left join sl_wcsi_EventRegister b on b.id = a.eventId and b.registerEventState=a.eventTaskState"
				+ " left join sl_dcms_eventType et on et.id = b.registerEventType "
				+ " left join sl_wcsi_gridArea ga on ga.id = b.registerEventToNetGrid "
				+ " left join sl_dcms_grading gr on gr.id = b.registerEventEvaluateLevel "
				+ " left join sl_dcms_option sl on sl.id = b.registerEventSeriousLevel "
				+ " left join sl_dcms_option o on o.id = b.registerEventState "
				//+ " left join sl_dcms_division d on d.id = ga.areaId "
				+ " where b.dealLimitSpan < a.diffTime and a.unfinished = '1' and (b.dealLimitSpan - a.diffTime) > 0 and (b.dealLimitSpan - a.diffTime) <= " + limit;
		
		if(type != null) {
			SQL1 += "and b.registerEventState='"+ type.getId() + "' ";
		}
		if(StringUtils.hasText(key)){
			SQL1+=" and (b.registerEventName like '%"+key+"%' or b.registerEventContent like '%"+key+"%' or o.sTitle like '%"+key+"%') ";	
		}
		
		
		String SQL = " select sq.* from  (select e2r.* from sl_wcsi_e2r e2r where e2r.roleId = '"+ roleId +"' ) er left join ( " + SQL1 + " ) sq on er.eventTypeId = sq.registerEventType where sq.id is not null ";
		
		SQL += " order by sq." + orderName + " " + ( StringUtils.hasText(dir) ? dir : "" );
		SQL += " limit "+ (int)(Math.floor(start / length) * length) + " , " +  (int)((Math.floor(start / length) + 1) * length);
		
		List<Map<String, Object>> resultList = this.jdbcTemplate.queryForList(SQL);
		
		return resultList;
	}

	@Override
	public int findCountUnderTimeoutEventByRole(String roleId, String key, double limit, Option type) {
		String SQL1 = "select b.*, "
				+ " a.eventTaskUserName, "
				+ " a.diffTime, "
				+ " et.eventName as 'eventTypeName', "
				+ " ga.gridName as 'gridName', "
				+ " o.sTitle as 'registerEventStateTitle', "
				+ " o.sTitle as 'registerEventSeriousLevelTitle', "
				+ " gr.gradingName as 'gradingName' "
				//+ " d.divisionName as 'divisionName' "
				+ " from ViewForTimeOutTask a "
				+ " left join sl_wcsi_EventRegister b on b.id = a.eventId and b.registerEventState=a.eventTaskState"
				+ " left join sl_dcms_eventType et on et.id = b.registerEventType "
				+ " left join sl_wcsi_gridArea ga on ga.id = b.registerEventToNetGrid "
				+ " left join sl_dcms_grading gr on gr.id = b.registerEventEvaluateLevel "
				+ " left join sl_dcms_option sl on sl.id = b.registerEventSeriousLevel "
				+ " left join sl_dcms_option o on o.id = b.registerEventState "
				//+ " left join sl_dcms_division d on d.id = ga.areaId "
				+ " where b.dealLimitSpan < a.diffTime and a.unfinished = '1' and (b.dealLimitSpan - a.diffTime) > 0 and (b.dealLimitSpan - a.diffTime) <= " + limit;
		
		if(type != null) {
			SQL1 += "and b.registerEventState='"+ type.getId() + "' ";
		}
		if(StringUtils.hasText(key)){
			SQL1+=" and (b.registerEventName like '%"+key+"%' or b.registerEventContent like '%"+key+"%' or o.sTitle like '%"+key+"%') ";	
		}
		
		String SQL = " select count(1) from (select e2r.* from sl_wcsi_e2r e2r where e2r.roleId = '"+ roleId +"' ) er left join ( " + SQL1 + " ) sq on er.eventTypeId = sq.registerEventType where sq.id is not null ";
		
		int count = 0;
		count = this.jdbcTemplate.queryForObject(SQL, Integer.class);
		
		return count;
	}

	@Override
	public int findCountUnderTimeoutEventByRole(String roleId, Option type) {
		
		String SQL1 = "select distinct b.* "
				+ " from ViewForTimeOutTask a "
				+ " left join sl_wcsi_EventRegister b on b.id = a.eventId and b.registerEventState=a.eventTaskState "
				+ " where b.registerEventState='"+ type.getId() +"' and b.acceptLimitSpan < a.diffTime "
				+ " and (TIME_TO_SEC(TIMEDIFF(now(),a.eventTaskTime)) > b.dealLimitSpan*2*3600/3 and TIME_TO_SEC(TIMEDIFF(now(),a.eventTaskTime)) < b.dealLimitSpan*3600 )";
		
		
		String SQL = " select count(1) from (select e2r.* from sl_wcsi_e2r e2r where e2r.roleId = '"+ roleId +"' ) er left join ( " + SQL1 + " ) sq on er.eventTypeId = sq.registerEventType where sq.id is not null ";
		
		int count = 0;
		count = this.jdbcTemplate.queryForObject(SQL, Integer.class);
		
		return count;
	}
	
	@Override
	public List<EventRegister> getPageByEventDate(int start, int length, Date begin, Date end, String orderName,
			Direction dir) {
		Page<EventRegister> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		
		pages = eventRegisterRepository.getPageByEventDate(begin, end, pageable);
		
		return pages.getContent();
	}

	@Override
	public int getCountByEventDate(Date begin, Date end) {
		int count = 0;
		count = eventRegisterRepository.getPageCountByEventDate(begin, end);
		return count;
	}

	@Override
	public ViewForEventRegisterTask getEventRegisterTaskByOpt(String opt, String id) {
		ViewForEventRegisterTask task = viewForEventRegisterTaskRepository.getByState(opt,id);
		return task;
	}

}